package net.technics;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import net.hibernate.HibernateUtil;
import net.models.Application;
import net.models.Praticiens;
import net.models.TypesPraticiens;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.property.Properties;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class PraticienAjout {
	private DataBindingContext		m_bindingContext;

	protected Shell					shlBackOfficeManagement;
	private Text					nomPraticien;
	private Text					prenomPraticien;
	private Text					adressePraticien;
	private Text					cpPraticien;
	private Text					villePraticien;
	private Text					txtAjouterUnUtilisateur;
	private List<TypesPraticiens>	typesPraticiens	= new ArrayList<TypesPraticiens>();
	private Combo					cmbTypePraticien;
	private ComboViewer				cmbViewerTypePra;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		Display display = Display.getDefault();
		Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
			public void run() {
				try {
					PraticienAjout window = new PraticienAjout();
					window.open();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Open the window.
	 */
	public void open() {
		typesPraticiens = Application.getTypesPraticiens();
		Display display = Display.getDefault();
		createContents();
		shlBackOfficeManagement.open();
		shlBackOfficeManagement.layout();
		while (!shlBackOfficeManagement.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shlBackOfficeManagement = new Shell();
		shlBackOfficeManagement.setText("Back Office Management");
		shlBackOfficeManagement.setSize(305, 334);

		Composite composite = new Composite(shlBackOfficeManagement, SWT.NONE);
		composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_BLUE));
		composite.setBounds(0, 0, 287, 48);

		txtAjouterUnUtilisateur = new Text(composite, SWT.NONE);
		txtAjouterUnUtilisateur.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.BOLD));
		txtAjouterUnUtilisateur.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		txtAjouterUnUtilisateur.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_BLUE));
		txtAjouterUnUtilisateur.setText("Ajouter un praticien");
		txtAjouterUnUtilisateur.setBounds(10, 10, 178, 21);

		Composite composite_1 = new Composite(shlBackOfficeManagement, SWT.NONE);
		composite_1.setBounds(10, 54, 277, 179);

		CLabel lblNom = new CLabel(composite_1, SWT.NONE);
		lblNom.setBounds(10, 10, 61, 21);
		lblNom.setText("Nom");

		CLabel lblPrnom = new CLabel(composite_1, SWT.NONE);
		lblPrnom.setText("Prénom");
		lblPrnom.setBounds(10, 37, 61, 21);

		CLabel lblAdresse = new CLabel(composite_1, SWT.NONE);
		lblAdresse.setText("Adresse");
		lblAdresse.setBounds(10, 64, 61, 21);

		CLabel lblCodePostal = new CLabel(composite_1, SWT.NONE);
		lblCodePostal.setText("Code postal");
		lblCodePostal.setBounds(10, 91, 86, 21);

		CLabel lblVille = new CLabel(composite_1, SWT.NONE);
		lblVille.setText("Ville");
		lblVille.setBounds(10, 118, 61, 21);

		CLabel lblGroupe = new CLabel(composite_1, SWT.NONE);
		lblGroupe.setText("Type de Praticien");
		lblGroupe.setBounds(10, 145, 125, 21);

		nomPraticien = new Text(composite_1, SWT.BORDER);
		nomPraticien.setBounds(141, 10, 127, 21);

		prenomPraticien = new Text(composite_1, SWT.BORDER);
		prenomPraticien.setBounds(141, 37, 127, 21);

		adressePraticien = new Text(composite_1, SWT.BORDER);
		adressePraticien.setBounds(141, 64, 127, 21);

		cpPraticien = new Text(composite_1, SWT.BORDER);
		cpPraticien.setBounds(141, 91, 127, 21);

		villePraticien = new Text(composite_1, SWT.BORDER);
		villePraticien.setBounds(141, 118, 127, 21);

		cmbViewerTypePra = new ComboViewer(composite_1, SWT.NONE);
		cmbTypePraticien = cmbViewerTypePra.getCombo();
		cmbTypePraticien.setBounds(141, 145, 127, 28);

		Composite composite_2 = new Composite(shlBackOfficeManagement, SWT.NONE);
		composite_2.setBounds(75, 239, 203, 48);

		Button button = new Button(composite_2, SWT.NONE);
		button.setText("Annuler");
		button.setBounds(106, 10, 90, 30);

		Button button_1 = new Button(composite_2, SWT.NONE);
		button_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				Session session = HibernateUtil.getSession();
				Transaction trans = session.beginTransaction();
				IStructuredSelection sel = (IStructuredSelection) (cmbViewerTypePra.getSelection());
				TypesPraticiens typesPraticien = (TypesPraticiens) (sel.iterator().next());
				Praticiens praticien = new Praticiens(nomPraticien.getText(), prenomPraticien.getText(), adressePraticien.getText(), cpPraticien.getText(), villePraticien.getText(), typesPraticien);
				session.persist(praticien);
				trans.commit();
				session.close();
			}
		});
		button_1.setText("Valider");
		button_1.setBounds(10, 10, 90, 30);
		m_bindingContext = initDataBindings();
	}

	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		ObservableListContentProvider listContentProvider = new ObservableListContentProvider();
		IObservableMap observeMap = BeansObservables.observeMap(listContentProvider.getKnownElements(), TypesPraticiens.class, "libelle");
		cmbViewerTypePra.setLabelProvider(new ObservableMapLabelProvider(observeMap));
		cmbViewerTypePra.setContentProvider(listContentProvider);
		//
		IObservableList selfList = Properties.selfList(TypesPraticiens.class).observe(typesPraticiens);
		cmbViewerTypePra.setInput(selfList);
		//
		return bindingContext;
	}
}
